package game;

import java.net.*;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SneakyBastard {
	private static ArrayList<Integer> teamNumbers;
	private static int[] balance;
	private static int[] turns;

	public static void main(String[] args) {
		teamNumbers = new ArrayList<Integer>();

		try {
			String urlName = "http://www.contest/event/gt/leaderboard";
			URL url = new URL(urlName);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String line = in.readLine();
			while (line != null) {
				if (line.contains("/team/")) {
					while (line.indexOf("/team/") != -1) {
						int start = line.indexOf("/team/");
						line = line.substring(start + 6);
						if (!line.equals(""))
							teamNumbers
									.add(Integer.parseInt(line.split("\"")[0]));
					}
				}
				line = in.readLine();
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			PrintWriter out = new PrintWriter(new FileWriter(new File(
					"twght.txt")));

			int max = 0;
			for (Integer n : teamNumbers) {
				if (n > max)
					max = n;
			}
			balance = new int[max];
			turns = new int[max];

			for (Integer n : teamNumbers) {
				sneakAHoe(n);
			}

			for (int i = 0; i < max; i++) {
				if (turns[i] != 0) {
					out.println("Player:" + i + ";Turns:" + turns[i]
							+ ";balanceFactor:" + balance[i]);
				}
			}

		} catch (IOException ioe) {
			System.err.println("Writing to file failed!");
		}
	}

	public static void sneakAHoe(int playerID) {
		ArrayList<Integer> rounds = new ArrayList<Integer>();
		String teamName = "";
		try {
			String urlName = "http://www.contest/team/" + playerID;
			URL url = new URL(urlName);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String line = in.readLine();
			line = in.readLine();
			line = line.split("View Team: ")[2];
			teamName = line.split("</h2>")[0];
			while (line != null) {
				if (line.contains("/round/")) {
					while (line.indexOf("/round/") != -1) {
						int start = line.indexOf("/round/");
						line = line.substring(start + 7);
						if (!line.equals(""))
							if (!rounds.contains(Integer.parseInt(line
									.split("\"")[0])))
								rounds.add(Integer.parseInt(line.split("\"")[0]));
					}
				}
				line = in.readLine();
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		for (Integer n : rounds) {
			sneakAGame(playerID, teamName, n);
			System.out.println(teamName + ": " + n);
		}
	}

	public static void sneakAGame(int playerID, String teamName, int roundID) {

		System.err.println("Checking game " + roundID);
		try {
			String urlName = "http://www.contest/game/" + roundID;
			URL url = new URL(urlName);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String line = in.readLine();
			
			while(line != null){
				System.out.println(line);
				line = in.readLine();
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
